package com.yanyilu.basing.controller;


import com.yanyilu.basing.common.R;
import com.yanyilu.basing.entity.Leaderboard;
import com.yanyilu.basing.mapper.LeaderboardMapper;
import com.yanyilu.basing.service.ILeaderboardService;
import com.yanyilu.basing.util.ParseJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *  排名控制
 * @author Sxx
 * @since 2020-10-26
 */
@CrossOrigin
@RestController
@RequestMapping("/leaderboard")
public class LeaderboardController {
    @Autowired
    ILeaderboardService leaderboardService;

    @Autowired
    LeaderboardMapper leaderboardMapper;

    @GetMapping("/frontend/list")
    public R getList(@RequestParam("limit")Integer limit,@RequestParam("page")Integer page){
        Integer userId = ParseJwtUtil.getUserId();
        return leaderboardService.getList(userId,limit,page);

    }
    /**
     *信息
     * @return
     */
    @GetMapping("/frontend/info")
    public R getLeaderBoardInfo(){
        Integer userId = ParseJwtUtil.getUserId();
        return leaderboardService.getInfo(userId);
    }

    /**
     * 管理端排行列表
     * @param page
     * @param limit
     * @param contactDetails
     * @return
     */
    @RequestMapping("/admin/list")
    public R adminList(@RequestParam("page")Integer page,
                       @RequestParam("limit")Integer limit,
                       @RequestParam(value = "contactDetails",required = false)String contactDetails){
        if ( null == contactDetails ){
            return leaderboardService.getList(page,limit);
        }
        return leaderboardService.adminList(page,limit,contactDetails);
    }

    /**
     * 核销
     * @param leaderboard
     * @return
     */

    @RequestMapping("/admin/update")
    public R adminUpdate(@RequestBody Leaderboard leaderboard){

        return leaderboardService.adminUpdate(leaderboard);
    }

    @GetMapping("/admin/delete")
    public R adminDelete(@RequestParam("leaderboardId") Integer leaderboardId){
        boolean b = leaderboardService.deleteById(leaderboardId);
        leaderboardMapper.updateRankByMil(0);
        return b ? R.ok() :R.error();
    }
}
